package com.yabiao.idempotent.token;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 *   Redis工具类
 * </p>
 *
 * @author: zhangyb
 * @create: 2020-09-13
 **/
@Component
public class RedisService {
    @Autowired
    private RedisTemplate redisTemplate;

    public boolean setEx(String key,Object value,Long expireTime){
        try {
            ValueOperations ops = redisTemplate.opsForValue();
            ops.set(key,value);
            redisTemplate.expire(key,expireTime, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    public boolean exists(String key){
        return redisTemplate.hasKey(key);
    }

    public boolean remove(String key){
        if (exists(key)){
            return redisTemplate.delete(key);
        }
        return false;
    }
}
